#pragma once
#include <brpc/server.h>
#include <utility>
#include "base.pb.h"
#include "user.pb.h"
#include "file.pb.h"
#include "message.pb.h"

#include "message-odb.hxx"

#include "etcd.hpp"
#include "channel.hpp"
#include "mysql_message.hpp"
#include "data_es.hpp"
#include "utils.hpp"
#include "rabbitmq.hpp"


class MsgStorageServiceImpl : public chat_system::MsgStorageService {
public:
    using ptr = std::shared_ptr<MsgStorageServiceImpl>;


    MsgStorageServiceImpl(
        std::shared_ptr<odb::core::database> mysql_client,
        std::shared_ptr<elasticlient::Client> es_client,
        ServiceManager::ptr service_channel,
        const std::string& user_service_name, 
        const std::string& file_service_name, 
        const std::string& exchange_name,
        const std::string& routing_key,
        RabbitMQClient::ptr rabbitmq_client) 
    : _message_table(std::make_shared<MessageTable>(mysql_client))
    , _es_message(std::make_shared<ESMessage>(es_client))
    , _service_channel(service_channel)
    , _user_service_name(user_service_name)
    , _file_service_name(file_service_name)
    , _exchange_name(exchange_name)
    , _routing_key(routing_key)
    , _rabbitmq_client(rabbitmq_client) {

        // 创建消息索引
        bool success = _es_message->createIndex();
        if(!success) {
            LOG_ERROR("创建消息索引失败");
            abort();
        }
        LOG_INFO("创建消息索引成功");
    }


    virtual void GetHistoryMsg(::google::protobuf::RpcController* controller,
                       const ::chat_system::GetHistoryMsgReq* request,
                       ::chat_system::GetHistoryMsgRsp* response,
                       ::google::protobuf::Closure* done) override {
        auto err_handle = [&request, &response](const std::string& err_msg) {
            response->set_request_id(request->request_id());
            response->set_success(false);
            response->set_errmsg(err_msg);
        };
        brpc::ClosureGuard guard(done); 
        LOG_INFO("收到获取历史消息请求: {}", request->ShortDebugString());

        std::string chat_session_id = request->chat_session_id();
        if(chat_session_id.empty()) {
            err_handle("会话ID不能为空");
            return;
        }
        int64_t start_time = request->start_time();
        int64_t end_time = request->over_time();   
        if(start_time == 0 || end_time == 0) {
            err_handle("开始时间和结束时间不能为空");
            return;
        }


        // 向 MySQL查询历史消息
        boost::posix_time::ptime stime = boost::posix_time::from_time_t(start_time);
        boost::posix_time::ptime etime = boost::posix_time::from_time_t(end_time);
        auto message_list = _message_table->range(chat_session_id, stime, etime);


        // 根据查询到的元数据, 向文件服务获取文件内容
        std::unordered_map<std::string, std::string> file_content;
        std::unordered_set<std::string> file_ids;
        for(const auto& msg : message_list) {
            if(!msg.file_id().empty()) {
                file_ids.emplace(msg.file_id());
            }
        }
        bool success = true;
        if(file_ids.empty() == false)
            success = _GetFile(request->request_id(), file_ids, file_content);
        if(!success) {
            err_handle("从文件服务获取文件内容失败");
            return;
        }

        
        // 根据查询到的元数据, 向用户管理服务获取用户信息
        std::unordered_map<std::string, chat_system::UserInfo> user_info_map;
        std::unordered_set<std::string> user_ids;
        for(const auto& msg : message_list) {
            if(!msg.user_id().empty()) {
                LOG_DEBUG("需要获取用户信息的用户ID: {}", msg.user_id());
                user_ids.emplace(msg.user_id());
            }
        }
        success = _GetUser(request->request_id(), user_ids, user_info_map);
        if(!success) {
            err_handle("从用户服务获取用户信息失败");
            return;
        }
        

        // 组织响应
        for(auto& msg : message_list) {
            // 设置MessageInfo
            chat_system::MessageInfo rsp_msg_info;
            rsp_msg_info.set_message_id(msg.message_id());
            rsp_msg_info.set_chat_session_id(msg.session_id());
            rsp_msg_info.set_timestamp(boost::posix_time::to_time_t(msg.create_time()));
            rsp_msg_info.mutable_sender()->CopyFrom(user_info_map[msg.user_id()]);
            // 设置MessageContent
            chat_system::MessageContent rsp_msg_content;
            rsp_msg_content.set_message_type(chat_system::MessageType(msg.type()));
            switch (msg.type()) {
                case chat_system::MessageType::STRING: {
                    rsp_msg_content.mutable_string_message()->set_content(msg.content());
                    break;
                }
                case chat_system::MessageType::IMAGE: {
                    auto* image_msg = rsp_msg_content.mutable_image_message();
                    image_msg->set_image_content(file_content[msg.file_id()]);
                    image_msg->set_file_id(msg.file_id());
                    break;
                }
                case chat_system::MessageType::FILE: {
                    auto* file_msg = rsp_msg_content.mutable_file_message();
                    file_msg->set_file_name(msg.file_name());
                    file_msg->set_file_size(msg.file_size());
                    file_msg->set_file_contents(file_content[msg.file_id()]);
                    file_msg->set_file_id(msg.file_id());
                    break;
                }
                case chat_system::MessageType::SPEECH: {
                    auto* speech_msg = rsp_msg_content.mutable_speech_message();
                    speech_msg->set_file_contents(file_content[msg.file_id()]);
                    speech_msg->set_file_id(msg.file_id());
                    break;
                }
                default: {
                    err_handle("不支持的消息类型");
                    LOG_ERROR("不支持的消息类型: {}", msg.type());
                    return;
                }
            }   
            rsp_msg_info.mutable_message()->CopyFrom(rsp_msg_content);
            response->mutable_msg_list()->Add(std::move(rsp_msg_info));
        }
        // 填充response
        response->set_request_id(request->request_id());
        response->set_success(true);

        LOG_INFO("根据时间段获取消息成功: {} 条", message_list.size());
    }   


    virtual void GetRecentMsg(::google::protobuf::RpcController* controller,
                       const ::chat_system::GetRecentMsgReq* request,
                       ::chat_system::GetRecentMsgRsp* response,
                       ::google::protobuf::Closure* done) override {
        auto err_handle = [&request, &response](const std::string& err_msg) {
            response->set_request_id(request->request_id());
            response->set_success(false);
            response->set_errmsg(err_msg);
        };
        brpc::ClosureGuard guard(done);


        // 获取关键信息
        std::string chat_session_id = request->chat_session_id();
        if(chat_session_id.empty()) {
            err_handle("会话ID不能为空");
            return;
        }
        int msg_count = request->msg_count();


        // 向 MySQL查询chae_session_id下的最近 msg_count 条消息
        auto message_list = _message_table->recent(chat_session_id, msg_count);
        if(message_list.empty()) {
            response->set_success(true);
            response->set_request_id(request->request_id());
            return;
        }


        // 根据查询到的元数据, 向文件服务获取文件内容
        std::unordered_map<std::string, std::string> file_content;
        std::unordered_set<std::string> file_ids;
        for(const auto& msg : message_list) {
            if(!msg.file_id().empty()) {
                file_ids.emplace(msg.file_id());
            }
        }
        bool success = true;
        if(file_ids.empty() == false)
            success = _GetFile(request->request_id(), file_ids, file_content);
        if(!success) {
            err_handle("从文件服务获取文件内容失败");
            return;
        }

        
        // 根据查询到的元数据, 向用户管理服务获取用户信息
        std::unordered_map<std::string, chat_system::UserInfo> user_info_map;
        std::unordered_set<std::string> user_ids;
        for(const auto& msg : message_list) {
            if(!msg.user_id().empty()) {
                LOG_DEBUG("需要获取用户信息的用户ID: {}", msg.user_id());
                user_ids.emplace(msg.user_id());
            }
        }
        success = _GetUser(request->request_id(), user_ids, user_info_map);
        if(!success) {
            err_handle("从用户服务获取用户信息失败");
            return;
        }
        

        // 组织响应
        for(auto& msg : message_list) {
            // 设置MessageInfo
            chat_system::MessageInfo rsp_msg_info;
            rsp_msg_info.set_message_id(msg.message_id());
            rsp_msg_info.set_chat_session_id(msg.session_id());
            rsp_msg_info.set_timestamp(boost::posix_time::to_time_t(msg.create_time()));
            rsp_msg_info.mutable_sender()->CopyFrom(user_info_map[msg.user_id()]);
            // 设置MessageContent
            chat_system::MessageContent rsp_msg_content;
            rsp_msg_content.set_message_type(chat_system::MessageType(msg.type()));
            switch (msg.type()) {
                case chat_system::MessageType::STRING: {
                    rsp_msg_content.mutable_string_message()->set_content(msg.content());
                    break;
                }
                case chat_system::MessageType::IMAGE: {
                    auto* image_msg = rsp_msg_content.mutable_image_message();
                    image_msg->set_image_content(file_content[msg.file_id()]);
                    image_msg->set_file_id(msg.file_id());
                    break;
                }
                case chat_system::MessageType::FILE: {
                    auto* file_msg = rsp_msg_content.mutable_file_message();
                    file_msg->set_file_name(msg.file_name());
                    file_msg->set_file_size(msg.file_size());
                    file_msg->set_file_contents(file_content[msg.file_id()]);
                    file_msg->set_file_id(msg.file_id());
                    break;
                }
                case chat_system::MessageType::SPEECH: {
                    auto* speech_msg = rsp_msg_content.mutable_speech_message();
                    speech_msg->set_file_contents(file_content[msg.file_id()]);
                    speech_msg->set_file_id(msg.file_id());
                    break;
                }
                default: {
                    err_handle("不支持的消息类型");
                    LOG_ERROR("不支持的消息类型: {}", msg.type());
                    return;
                }
            }   
            rsp_msg_info.mutable_message()->CopyFrom(rsp_msg_content);
            response->mutable_msg_list()->Add(std::move(rsp_msg_info));
        }
        // 填充response
        response->set_request_id(request->request_id());
        response->set_success(true);

        LOG_INFO("获取最近消息成功: {} 条", message_list.size());
    }


    // 针对STRING类型的消息进行关键词搜索
    virtual void MsgSearch(::google::protobuf::RpcController* controller,
                       const ::chat_system::MsgSearchReq* request,
                       ::chat_system::MsgSearchRsp* response,
                       ::google::protobuf::Closure* done) override {
        auto err_handle = [&request, &response](const std::string& err_msg) {
            response->set_request_id(request->request_id());
            response->set_success(false);
            response->set_errmsg(err_msg);
        };
        brpc::ClosureGuard guard(done);

        // 获取关键信息
        std::string chat_session_id = request->chat_session_id();
        std::string search_key = request->search_key();
        if(chat_session_id.empty()) {
            err_handle("会话ID不能为空");
            return;
        }


        // 向ES查询消息
        auto message_list = _es_message->search(chat_session_id, search_key);
        if(message_list.empty()) {
            response->set_success(true);
            response->set_request_id(request->request_id());
            return;
        }


        // 根据查询到的元数据, 向用户管理服务获取用户信息
        std::unordered_map<std::string, chat_system::UserInfo> user_info_map;
        std::unordered_set<std::string> user_ids;
        for(const auto& msg : message_list) {
            if(!msg.user_id().empty()) {
                user_ids.emplace(msg.user_id());
            }
        }
        bool success = _GetUser(request->request_id(), user_ids, user_info_map);
        if(!success) {
            err_handle("从用户服务获取用户信息失败");
            return;
        }


        // 组织响应
        for(auto& msg : message_list) {
            // 设置MessageInfo
            chat_system::MessageInfo rsp_msg_info;
            rsp_msg_info.set_message_id(msg.message_id());
            rsp_msg_info.set_chat_session_id(msg.session_id());
            rsp_msg_info.set_timestamp(boost::posix_time::to_time_t(msg.create_time()));
            rsp_msg_info.mutable_sender()->CopyFrom(user_info_map[msg.user_id()]);
            // 设置MessageContent
            chat_system::MessageContent rsp_msg_content;
            rsp_msg_content.set_message_type(chat_system::MessageType(chat_system::MessageType::STRING));
            rsp_msg_content.mutable_string_message()->set_content(msg.content());

            rsp_msg_info.mutable_message()->CopyFrom(rsp_msg_content);

            // 添加到响应
            response->mutable_msg_list()->Add(std::move(rsp_msg_info));
        }
        response->set_request_id(request->request_id());
        response->set_success(true);
        LOG_INFO("消息搜索成功: {} 条", message_list.size());
    }
    
    void onMessage(const char* body, size_t size) {
        chat_system::MessageInfo message;
        bool success = message.ParseFromArray(body, size);
        if(!success) {
            LOG_ERROR("反序列化消息失败");
            return;
        }
        LOG_INFO("收到消息: {}", message.ShortDebugString());

        std::string content;
        std::string file_id;
        std::string file_name;
        int file_size;
        chat_system::MessageType message_type = message.message().message_type();


        // 根据消息类型进行处理
        switch(message_type) {
            case chat_system::MessageType::STRING: {
                content = message.message().string_message().content();
                LOG_DEBUG("处理文本消息: {}", content);
                success = _es_message->appendData(message.message_id(), 
                                        message.chat_session_id(), 
                                        message.sender().user_id(),
                                        message.timestamp(), 
                                        content);
                if(!success) {
                    LOG_ERROR("将文本消息存储到ES失败");
                    return;
                }
                break;
            }
            case chat_system::MessageType::IMAGE: {
                const auto& msg = message.message().image_message();
                LOG_DEBUG("图像文件内容 {}------------------------------", msg.image_content());
                success = _PutFile("", msg.image_content(), msg.image_content().size(), file_id);
                if(!success) {
                    LOG_ERROR("上传图片到文件服务失败");
                    return;
                }
                break;
            }
            case chat_system::MessageType::FILE: {
                const auto& msg = message.message().file_message();
                file_name = msg.file_name();
                file_size = msg.file_size();
                LOG_DEBUG("文件内容 {}------------------------------", msg.file_contents());
                success = _PutFile(file_name, msg.file_contents(), file_size, file_id);
                if(!success) {
                    LOG_ERROR("上传文件到文件服务失败");
                    return;
                }
                break;
            }
            case chat_system::MessageType::SPEECH: {
                const auto& msg = message.message().speech_message();
                LOG_DEBUG("语音文件内容 {}------------------------------", msg.file_contents());
                success = _PutFile("", msg.file_contents(), msg.file_contents().size(), file_id);
                if(!success) {
                    LOG_ERROR("上传语音到文件服务失败");
                    return;
                }
                break;
            }
            default: {
                LOG_ERROR("不支持的消息类型: {}", message_type);
                return;
            }
        }


        // 将消息存储到MySQL
        boost::posix_time::ptime create_time(boost::posix_time::from_time_t(message.timestamp()));
        Message sql_msg(message.message_id(), 
                        message.chat_session_id(), 
                        message.sender().user_id(),
                        message_type,
                        create_time);
        sql_msg.set_content(content);
        sql_msg.set_file_id(file_id);
        sql_msg.set_file_name(file_name);
        sql_msg.set_file_size(file_size);
        success = _message_table->insert(sql_msg);
        if(!success) {
            LOG_ERROR("将消息存储到MySQL失败");
            return;
        }
        LOG_INFO("消息存储到MySQL成功, 消息ID: {}", sql_msg.message_id());

    }
private:
    bool _GetUser(const std::string &req_id,
                  const std::unordered_set<std::string> &user_ids,
                  std::unordered_map<std::string, chat_system::UserInfo> &user_info) {
        //获取用户服务信道管理对象
        auto channel_manager = _service_channel->choose(_user_service_name);
        if(channel_manager == nullptr) {
            LOG_ERROR("获取用户服务信道失败");
            return false;
        }
        // 获取用户服务信道
        auto channel = channel_manager->choose();
        if(channel == nullptr) {
            LOG_ERROR("获取用户服务信道失败");
            return false;
        }
        
        
        // 向用户管理子服务发起RPC调用, 获取用户信息
        chat_system::UserService_Stub stub(channel.get());
        chat_system::GetMultiUserInfoReq req;
        chat_system::GetMultiUserInfoRsp rsp;
        req.set_request_id(req_id);
        for(const auto& user_id : user_ids) {
            if(user_id.empty()) continue;
            req.add_users_id(user_id);
        }

        brpc::Controller cntl;  
        stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
        LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
        if(cntl.Failed() || !rsp.success()) {
            LOG_ERROR("获取用户信息失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
            return false;
        }

        // 获取用户信息成功
        for(const auto& user : rsp.users_info()) {
            user_info.insert(std::make_pair(user.first, user.second));
        }
        return true;
    }   

    
    bool _GetFile(const std::string &req_id, 
                  const std::unordered_set<std::string> &file_ids, 
                  std::unordered_map<std::string, std::string> &file_content) {
        //获取文件服务信道管理对象
        auto channel_manager = _service_channel->choose(_file_service_name);
        if(channel_manager == nullptr) {
            LOG_ERROR("获取文件服务信道失败");
            return false;
        }
        // 获取文件服务信道
        auto channel = channel_manager->choose();
        if(channel == nullptr) {
            LOG_ERROR("获取文件服务信道失败");
            return false;
        }

        // 向文件子服务发起RPC调用, 下载文件
        chat_system::FileService_Stub stub(channel.get());
        chat_system::GetMultiFileReq req;
        chat_system::GetMultiFileRsp rsp;
        req.set_request_id(req_id);
        for(const auto& file_id : file_ids) {
            if(file_id.empty()) continue;
            req.add_file_id_list(file_id);
        }
        brpc::Controller cntl;
        stub.GetMultiFile(&cntl, &req, &rsp, nullptr);
        if(cntl.Failed() || !rsp.success()) {
            LOG_ERROR("从文件服务下载文件失败: {}", cntl.ErrorText());
            return false;
        }

        // 文件下载成功, 获取文件内容
        for(const auto& file_info : rsp.file_data()) {
            file_content.insert(std::make_pair(file_info.first, file_info.second.file_content()));
        }
        return true;
    }


    bool _PutFile(const std::string &filename,
                  const std::string &body, 
                  const int64_t fsize,
                  std::string &file_id) {
        //获取文件服务信道管理对象
        auto channel_manager = _service_channel->choose(_file_service_name);
        if(channel_manager == nullptr) {
            LOG_ERROR("获取文件服务信道失败");
            return false;
        }
        // 获取文件服务信道
        auto channel = channel_manager->choose();
        if(channel == nullptr) {
            LOG_ERROR("获取文件服务信道失败");
            return false;
        }


        // 向文件子服务发起RPC调用, 上传文件
        chat_system::FileService_Stub stub(channel.get());
        chat_system::PutSingleFileReq req;
        chat_system::PutSingleFileRsp rsp;
        req.mutable_file_data()->set_file_name(filename);
        req.mutable_file_data()->set_file_size(body.size());
        req.mutable_file_data()->set_file_content(body);
        brpc::Controller cntl;
        stub.PutSingleFile(&cntl, &req, &rsp, nullptr);
        if(cntl.Failed() || !rsp.success()) {
            LOG_ERROR("上传文件到文件服务失败: {}", cntl.ErrorText());
            return false;
        }

        // 文件上传成功, 获取文件id
        file_id = rsp.file_info().file_id();
        LOG_INFO("上传文件到文件服务成功, 文件id: {}", file_id);
        return true;
    }
private:
    MessageTable::ptr _message_table;
    ESMessage::ptr _es_message;
    
    // 用户服务信道管理对象
    ServiceManager::ptr _service_channel;
    std::string _user_service_name;
    std::string _file_service_name;

    // RabbitMQ客户端
    std::string _exchange_name;
    std::string _routing_key;
    RabbitMQClient::ptr _rabbitmq_client;

};


class MessageServer {
public:
    using ptr = std::shared_ptr<MessageServer>;

    MessageServer(Registry::ptr registry_client,
                Discovery::ptr discovery_client,
                std::shared_ptr<odb::core::database> mysql_client,
                std::shared_ptr<elasticlient::Client> es_client,
                RabbitMQClient::ptr rabbitmq_client,
                std::shared_ptr<brpc::Server> server)
    : _registry_client(registry_client)
    , _discovery_client(discovery_client)
    , _mysql_client(mysql_client)
    , _es_client(es_client)
    , _rabbitmq_client(rabbitmq_client)
    , _server(server) {}


    void start() {
        LOG_INFO("消息存储服务已启动");
        _server->RunUntilAskedToQuit();
    }

private:
    Registry::ptr _registry_client;
    Discovery::ptr _discovery_client;

    std::shared_ptr<odb::core::database> _mysql_client;
    std::shared_ptr<elasticlient::Client> _es_client;
    std::shared_ptr<elasticlient::Client> _client;
    RabbitMQClient::ptr _rabbitmq_client;
    std::shared_ptr<brpc::Server> _server;
};


class MessageServerBuilder { 
public:
    void make_rabbitmq_object(const std::string &user,
                   const std::string &password,
                   const std::string &host, 
                   const std::string &exchange_name,
                   const std::string &queue_name, 
                   const std::string &bind_key) {
        _rabbitmq_client = std::make_shared<RabbitMQClient>(user, password, host);
        _exchange_name = exchange_name;
        _queue_name = queue_name;
        _routing_key = bind_key;
        _rabbitmq_client->declareComponents(_exchange_name, queue_name, _routing_key);
    }


    void make_reg_object(const std::string &etcd_host, const std::string& service_name, const std::string service_host) {
        _registry_client = std::make_shared<Registry>(etcd_host);
        bool success = _registry_client->registry(service_name, service_host);
        if(!success)
        {
            LOG_ERROR("注册服务到etcd失败");
            return;
        }
        LOG_INFO("注册服务{} - {}到etcd成功", service_name, service_host);
    }


    void make_es_object(const std::vector<std::string>& es_addr_list) {
        _es_client = ESClientFactory::create(es_addr_list); 
    }




    void make_rpc_object(uint16_t port, int32_t timeout, uint8_t num_threads) {
        logging::LoggingSettings tmp_log;
        tmp_log.logging_dest = logging::LOG_TO_NONE;
        logging::InitLogging(tmp_log);
        _server = std::make_shared<brpc::Server>();
        int ret = _server->AddService(_message_service_impl.get(), brpc::SERVER_DOESNT_OWN_SERVICE);
        if(ret < 0)
        {
            LOG_ERROR("注册服务失败");
            return;
        }
        brpc::ServerOptions options;
        options.idle_timeout_sec = timeout;
        options.num_threads = num_threads;
        ret = _server->Start(port, &options);
        if(ret < 0)
        {
            LOG_ERROR("启动服务器失败");
            abort();  
        }
    }

    // 
    void make_discovery_object(const std::string &etcd_host, const std::string &basedir, const std::string &user_service_name, const std::string& file_service_name) {
        _user_service_name = basedir + user_service_name;
        _file_service_name = basedir + file_service_name;
        _service_manager = std::make_shared<ServiceManager>();     
        _service_manager->declared(_user_service_name);   
        _service_manager->declared(_file_service_name);
        auto put_cb = std::bind(&ServiceManager::onServiceOnline, _service_manager, std::placeholders::_1, std::placeholders::_2);
        auto del_cb = std::bind(&ServiceManager::onServiceOffline, _service_manager, std::placeholders::_1, std::placeholders::_2);
        _discovery_client = std::make_shared<Discovery>(etcd_host, basedir, put_cb, del_cb);
        if(_discovery_client == nullptr)
        {
            LOG_ERROR("创建服务发现对象失败");
            abort();
        }
        
    }


    void make_mysql_object(
        const std::string &user,
        const std::string &pswd,
        const std::string &host,
        const std::string &db,
        const std::string &cset,
        int port,
        int conn_pool_count) {
        _mysql_client = ODBFactory::create(user, pswd, db, host, port, cset, conn_pool_count);
    }


    void make_message_service_impl() {
        _message_service_impl = std::make_shared<MsgStorageServiceImpl>(_mysql_client, _es_client,
        _service_manager, _user_service_name, _file_service_name,
        _exchange_name, _routing_key, _rabbitmq_client);

        _rabbitmq_client->consume(_queue_name, [this](const char* body, size_t size) {
            _message_service_impl->onMessage(body, size);
        });
    }


     MessageServer::ptr build() {
        if(_message_service_impl == nullptr)
        {
            LOG_ERROR("用户服务实例未创建");
            return nullptr;
        }
        if(_registry_client == nullptr)
        {
            LOG_ERROR("注册中心对象未创建");
            return nullptr;
        }
        if(_server == nullptr)
        {
            LOG_ERROR("RPC服务器对象未创建");
            return nullptr;
        }


        if(_discovery_client == nullptr)
        {
            LOG_ERROR("服务发现对象未创建");
            return nullptr;
        }

        return std::make_shared<MessageServer>(_registry_client, _discovery_client, _mysql_client, _es_client, _rabbitmq_client, _server);
    }
private:
    std::string _user_service_name;
    std::string _file_service_name;

    MsgStorageServiceImpl::ptr _message_service_impl;
    
    Registry::ptr _registry_client;
    Discovery::ptr _discovery_client;
    ServiceManager::ptr _service_manager;

    std::shared_ptr<odb::core::database> _mysql_client;
    std::shared_ptr<elasticlient::Client> _es_client;
    std::string _exchange_name;
    std::string _routing_key;
    std::string _queue_name;
    RabbitMQClient::ptr _rabbitmq_client;

    std::shared_ptr<brpc::Server> _server;
};    
